﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kay.LazarToDo.Win.AppServices;

namespace Kay.LazarToDo.Win._Forms
{
    /// <summary>
    /// 添加待办事项
    /// </summary>
    public partial class AddLazarToDo : Form
    {
        private readonly ProjectAppService _projectAppService;

        private readonly LazarToDoAppService _lazarToDoAppService;

        /// <summary>
        /// 初始化添加待办事项
        /// </summary>
        public AddLazarToDo()
        {
            InitializeComponent();
            _projectAppService = new ProjectAppService();
            _lazarToDoAppService = new LazarToDoAppService();
        }

        private void AddLazarToDo_Load(object sender, EventArgs e)
        {
            Application.DoEvents();
            BindingProject();
        }

        /// <summary>
        /// 绑定项目
        /// </summary>
        public void BindingProject()
        {
            var data = _projectAppService.GetByShown(true);
            comboBoxProject.DataSource = data.ToList();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            var toDo = textBoxToDo.Text.Trim();
            if (string.IsNullOrWhiteSpace(toDo))
            {
                errorProvider.SetError(textBoxToDo, "请填写待办内容！");
                errorProvider.SetIconPadding(textBoxToDo, -15);
                return;
            }
            var projectId = (Guid)comboBoxProject.SelectedValue;
            if (_lazarToDoAppService.CheckName(toDo, projectId))
            {
                errorProvider.SetError(textBoxToDo, "待办内容已存在！");
                errorProvider.SetIconPadding(textBoxToDo, -15);
                return;
            }
            var toDoEntity = _lazarToDoAppService.SaveToDo(toDo, projectId);
            if (toDoEntity != null)
            {
                DialogResult = DialogResult.OK;
            }
        }
    }
}
